package com.example.leetdemo.plan1;

public class Sword047 {

    public TreeNode pruneTree(TreeNode root) {
        return dfsTree(root) ? null : root;
    }

    private boolean dfsTree(TreeNode th) {
        if (th == null) {
            return true;
        }
        if (dfsTree(th.left)) {
            th.left = null;
        }

        if (dfsTree(th.right)) {
            th.right = null;
        }

        if (th.left != null || th.right != null) {
            return false;
        } else if (th.val == 0) {
            return true;
        }

        return false;
    }

}
